package com.hjm.project.agentInfo.agentPolicy.service.impl;

import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.LogUtils;
import com.hjm.project.ActActive.ActActive.domain.ActActive;
import com.hjm.project.ActActive.ActActive.mapper.ActActiveMapper;
import com.hjm.project.agentInfo.agentActActive.utils.AgentPriceCommon;
import com.hjm.project.agentInfo.agentPolicy.domain.AgentPolicy;
import com.hjm.project.agentInfo.agentPolicy.mapper.AgentPolicyMapper;
import com.hjm.project.agentInfo.agentPolicy.service.IAgentPolicyService;
import org.springframework.stereotype.Service;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 代理结算底价Service业务层处理
 * 
 * @author maker-platform
 * @date 2024-03-21
 */
@Service
public class AgentPolicyServiceImpl implements IAgentPolicyService {
    @Resource
    private AgentPolicyMapper agentPolicyMapper;
    @Resource
    private ActActiveMapper actActiveMapper;

    /**
     * 查询代理结算底价
     * 
     * @param id 代理结算底价ID
     * @return 代理结算底价
     */
    @Override
    public AgentPolicy selectAgentPolicyById(Integer id) {
        return agentPolicyMapper.selectAgentPolicyById(id);
    }

    /**
     * 查询代理结算底价列表
     * 
     * @param agentPolicy 代理结算底价
     * @return 代理结算底价
     */
    @Override
    public List<AgentPolicy> selectAgentPolicyList(AgentPolicy agentPolicy) {
        return agentPolicyMapper.selectAgentPolicyList(agentPolicy);
    }

    /**
     * 查询代理结算底价列表
     *
     * @param agentPolicy 代理结算底价
     * @return 代理结算底价 一个结果
     */
    @Override
    public AgentPolicy selectAgentPolicyOne(AgentPolicy agentPolicy) {
        return agentPolicyMapper.selectAgentPolicyOne(agentPolicy);
    }

    /**
     * 新增代理结算底价
     * 
     * @param agentPolicy 代理结算底价
     * @return 结果
     */
    @Override
    public int insertAgentPolicy(AgentPolicy agentPolicy) {
        agentPolicy.setCreateTime(DateUtils.getNowDate());
        return agentPolicyMapper.insertAgentPolicy(agentPolicy);
    }

    /**
     * 修改代理结算底价
     * 
     * @param agentPolicy 代理结算底价
     * @return 结果
     */
    @Override
    public int updateAgentPolicy(AgentPolicy agentPolicy) {
        return agentPolicyMapper.updateAgentPolicy(agentPolicy);
    }

    /**
     * 删除代理结算底价对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAgentPolicyByIds(String ids) {
        return agentPolicyMapper.deleteAgentPolicyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除代理结算底价信息
     * 
     * @param id 代理结算底价ID
     * @return 结果
     */
    @Override
    public int deleteAgentPolicyById(Integer id) {
        return agentPolicyMapper.deleteAgentPolicyById(id);
    }

    @Override
    @Transactional
    public void saveAgentPrice(AgentPriceCommon agentPriceCommon) {
        try {
            //清空代理底价信息
            AgentPolicy ap = new AgentPolicy();
            ap.setActId(agentPriceCommon.getActId());
            ap.setAgentNum(agentPriceCommon.getAgentNum());
            agentPolicyMapper.deletePrice(ap);
            List<AgentPolicy> agentPolicies = AgentPriceCommon.viewToBean(agentPriceCommon);
            ActActive actActive = actActiveMapper.selectActActiveById(agentPriceCommon.getActId());
            if (actActive == null) {
                throw new BusinessException("政策【" + actActive.getActId() + "】不存在,请联系管理员");
            } else if (actActive.getStatus().intValue() != 0){
                throw new BusinessException("政策【" + actActive.getActId() + "】不存在,请联系管理员");
            }
            for (AgentPolicy aps : agentPolicies) {
                aps.setActId(agentPriceCommon.getActId());
                int i = agentPolicyMapper.insertAgentPolicy(aps);
                if (i == 0) {
                    throw new BusinessException("配置结算失败");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.logError("设置结算",e);
            throw e;
        }
    }
}
